package cz.data.domain.system.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import cz.data.common.base.BaseController;
import cz.data.common.base.BaseResponse;
import cz.data.common.base.SimpleDataResponse;
import cz.data.common.core.JsonPage;
import cz.data.common.validate.ValidationGroups;
import cz.data.domain.system.model.dto.DictItemDto;
import cz.data.domain.system.model.entity.DictItemEntity;
import cz.data.domain.system.model.query.DictItemQuery;
import cz.data.domain.system.model.vo.DictItemVo;
import cz.data.domain.system.mapstruct.DictItemMapper;
import cz.data.domain.system.service.DictItemService;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "系统管理-字典项")
@RestController
@RequestMapping("/dict/items")
public class DictItemController extends BaseController {

    @Autowired
    private DictItemService dictItemService;

    @Autowired
    private DictItemMapper dictItemMapper;

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "系统管理-字典项-详情", notes = "根据url的id来获取详细信息")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path")
    @GetMapping("/{id}")
    public SimpleDataResponse<DictItemVo> getDictItemById(@PathVariable String id) {
        DictItemEntity dictItemEntity = dictItemService.getById(id);
        return new SimpleDataResponse<>(dictItemMapper.toVO(dictItemEntity));
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "系统管理-字典项-分页查询", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dictItemQuery", value = "查询实体dictItemQuery", required = true, dataTypeClass = DictItemQuery.class)
    })
    @GetMapping("/page")
    public SimpleDataResponse<JsonPage<DictItemVo>> getDictItemPage(DictItemQuery dictItemQuery) {
        QueryWrapper<DictItemEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(dictItemQuery.getItemText()), "item_text", dictItemQuery.getItemText());
        queryWrapper.like(StringUtils.isNotBlank(dictItemQuery.getItemValue()), "item_value", dictItemQuery.getItemValue());
        queryWrapper.eq(StringUtils.isNotBlank(dictItemQuery.getDictId()), "dict_id", dictItemQuery.getDictId());
        IPage<DictItemEntity> page = dictItemService.page(new Page<>(dictItemQuery.getPageNum(), dictItemQuery.getPageSize()), queryWrapper);
        List<DictItemVo> collect = page.getRecords().stream().map(dictItemMapper::toVO).collect(Collectors.toList());
        JsonPage<DictItemVo> jsonPage = new JsonPage<>(page.getCurrent(), page.getSize(), page.getTotal(), collect);
        return new SimpleDataResponse<>(jsonPage);
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "系统管理-字典项-添加", notes = "根据dictItem对象添加信息")
    @ApiImplicitParam(name = "dictItem", value = "详细实体dictItem", required = true, dataType = "DictItemDto")
    @PostMapping()
    public SimpleDataResponse<DictItemVo> saveDictItem(@RequestBody @Validated({ValidationGroups.Insert.class}) DictItemDto dictItem) {
        DictItemEntity dictItemEntity = dictItemService.saveDictItem(dictItem);
        return new SimpleDataResponse<>(dictItemMapper.toVO(dictItemEntity));
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "系统管理-字典项-修改", notes = "根据url的id来指定修改对象，并根据传过来的信息来修改详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "dictItem", value = "详细实体dictItem", required = true, dataType = "DictItemDto")
    })
    @PutMapping("/{id}")
    public SimpleDataResponse<DictItemVo> updateDictItem(@PathVariable String id, @RequestBody @Validated({ValidationGroups.Update.class}) DictItemDto dictItem) {
        DictItemEntity dictItemEntity = dictItemService.updateDictItem(dictItem);
        return new SimpleDataResponse<>(dictItemMapper.toVO(dictItemEntity));
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "系统管理-字典项-删除", notes = "根据url的id来指定删除对象")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path")
    @DeleteMapping("/{id}")
    public BaseResponse deleteDictItemById(@PathVariable String id) {
        dictItemService.deleteDictItemById(id);
        return BaseResponse.success();
    }

    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "系统管理-字典项-批量删除", notes = "根据url的ids来批量删除对象")
    @ApiImplicitParam(name = "ids", value = "ID集合", required = true, dataType = "List", paramType = "path")
    @DeleteMapping("/batch/{ids}")
    public BaseResponse deleteDictItemBatch(@PathVariable List<String> ids) {
        dictItemService.deleteDictItemBatch(ids);
        return BaseResponse.success();
    }
}
